﻿
using JinfuyuanProject.Entity;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Lifetime;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Xml.Linq;
using JinfuyuanProject.Entity.ViewModel;

namespace JinfuyuanProject.Controllers
{
    public class PackageController : Controller
    {
        private readonly IPackageService _packageService;
        private readonly IServicesService _services;
        private readonly IPackageServicesService _packageServicesService;

        public PackageController(IPackageService packageService, IServicesService services,IPackageServicesService packageServicesService ) {
            _packageService=packageService;
            _services =services;
            _packageServicesService=packageServicesService;
        }
        // GET: Package
        #region 视图
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PackageList()
        {
            return View();
        }
      
        public ActionResult PackageListDetail(int Id =0)
        {
            List<Services> services = _services.GetList().ToList();
            List<PackageServices> packageServices = _packageServicesService.GetList().Where(x => x.ComboId == Id).ToList();
            var serviceIdsInPackage = packageServices.Select(ps => ps.ServiceId).ToList();
            List<ServicesVM> servicesVMs = new List<ServicesVM>();
            foreach (var service in services)
            {
                ServicesVM serviceVM = new ServicesVM
                {
                    Id = service.Id,
                    ServiceName = service.ServiceName,
                    ServicePrice = service.ServicePrice,
                    SerStartTime = service.SerStartTime,
                    ServiceInterval = service.ServiceInterval,
                    ServiceThreshold = service.ServiceThreshold,
                    ServiceType = service.ServiceType,
                    IsDelete = service.IsDelete,
                    CreateTime = service.CreateTime,
                    ServiceCount = 0,
                    IsChecked = serviceIdsInPackage.Contains(service.Id) 
                };
                var packageService = packageServices.FirstOrDefault(ps => ps.ServiceId == service.Id);
                if (packageService != null)
                {
                    serviceVM.ServiceCount = packageService.ServiceNumber;
                }
                servicesVMs.Add(serviceVM);
            }
            ViewBag.Services = servicesVMs;
            ViewBag.Id = Id;
            return View();
        }
        #endregion
        #region 接口
        public JsonResult GetPackageList(int page, int limit)
        {
          PackageFilter filter = new PackageFilter();   
            filter.limit = limit;   
            filter.page = page;
            return Json(_packageService.Query(filter), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult PackageInsert(Package package , List<PackageServices> packageServices)
        {
            package.CreateTime = DateTime.Now;
            package.IsDelete = 0;
            Hashtable ht = new Hashtable();
            if (!ModelState.IsValid)
            {
                ht["code"] = 1;
                ht["msg"] = "实体校验不合格";
            }
            _packageService.Insert(package);
            foreach (var PS in packageServices)
            {
                PS.IsDelete = 0;
                PS.CreateTime = DateTime.Now;
                PS.ComboId = package.Id;
            }
            _packageServicesService.InsertBatch(packageServices);
            ht["code"] = 0;
            ht["msg"] = "success";
            return Json(ht);
        }
        [HttpPost]
        public ActionResult PackageUpdate(Package package, List<PackageServices> packageServices)
        {
            int pID= package.Id;
            List<PackageServices> packageServices1 = _packageServicesService.GetList().Where(x => x.ComboId == pID).ToList();
            _packageServicesService.DeleteBatch(packageServices1);
            Hashtable ht = new Hashtable();
            if (!ModelState.IsValid)
            {
                ht["code"] = 1;
                ht["msg"] = "实体校验不合格";
                return Json(ht);
            }
            var package1 = _packageService.GetModel(package.Id);
            package1.PackageName = package.PackageName;
            package1.PackagePrice = package.PackagePrice;
            _packageService.Update(package1);
            foreach (var PS in packageServices)
            {
                PS.IsDelete = 0;
                PS.CreateTime = DateTime.Now;
                PS.ComboId = package1.Id;

            }
            _packageServicesService.InsertBatch(packageServices);
            ht["code"] = 0;
            ht["msg"] = "success";
            return Json(ht);
        }
        [HttpPost]
        public JsonResult GetPackageDetail(int Id)
        {
            Hashtable ht = new Hashtable();
            Package package = _packageService.GetModel(Id);
            if (package == null)
            ht["code"] = 1;
            ht["code"] = 0;
            ht["data"] = package;
            return Json(ht);
        }
        #endregion
    }
}